package com.sj.uabrowser.mvp.main.loader;

import com.sj.uabrowser.framework.BaseLoader;
import com.sj.uabrowser.framework.BasePresenter;
import com.sj.uabrowser.mvp.main.bean.HomeTab;
import com.sj.uabrowser.mvp.main.bean.HomeTabs;
import com.sj.uabrowser.mvp.main.bean.News;
import com.sj.uabrowser.mvp.main.bean.PageBean;
import com.sj.uabrowser.net.AuthResultClient;
import com.sj.uabrowser.net.ServiceApi;

import java.util.List;
import java.util.TreeMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class NewsLoader extends BaseLoader {

    public NewsLoader(BasePresenter mPresenter) {
        super(mPresenter);
    }

    public void fetchNewsTab(int requestId,TreeMap<String,Object>params){
        AuthResultClient.getService(ServiceApi.class)
                .GetNewsTab(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<PageBean<HomeTab>>() {
                    @Override
                    public void accept(PageBean<HomeTab> news) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBack(0, news);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        RxJavaPlugins.onError(throwable);
                        if(mPresenter!=null)
                            mPresenter.onDataCallBackError(requestId, throwable);
                    }
                });
    }

    public void fetchNewsData(int requestId,TreeMap<String,Object>params){
        AuthResultClient.getService(ServiceApi.class)
                .GetNewsData(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<PageBean<News>>() {
                    @Override
                    public void accept(PageBean<News> news) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBack(0, news);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        RxJavaPlugins.onError(throwable);
                        if(mPresenter!=null)
                            mPresenter.onDataCallBackError(requestId, throwable);
                    }
                });
    }
}
